const express = require('express');

// 实体类
const User = require('../entity/user/User');
const UserUpdateInfo = require('../entity/user/UserUpdateInfo');
const UserUpdateInfoAndPassword = require('../entity/user/UserUpdateInfoAndPassword');
const UserInfoUpdate = require('../entity/user/UserInfoUpdate');
const Response = require('../entity/http/Response');
const ObjId = require('../entity/common/ObjId');

// 业务层
const userService = require('../service/user');

const router = express.Router();

/**
 * @apiName 确认当前登录状态接口
 */
router.get('/checkLogin', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /user/checkLogin`);
    let result = new Response().success({ login: true });
    console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 确认当前登录状态响应 ${ JSON.stringify(result) }`);
    return res.status(200).json(result);
});

/**
 * @apiName 获取当前账号信息接口
 */
router.get('/info', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /user/info`);
    const { uid } = req.token_payload.data;
    // 进入业务层
    userService.getSelfInfo(req.requestId, uid).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取当前账号信息响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取当前账号信息时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 获取用户信息接口
 */
router.get('/userinfo', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /user/userinfo`);
    const { uid } = req.token_payload.data;
    let user;
    // 读取用户Id
    try {
        user = new ObjId(req.query.id);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "用户Id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 用户Id ${ JSON.stringify(user) }`);
    // 进入业务层
    userService.getUserInfo(req.requestId, uid, user).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取用户信息响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取用户信息时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 获取用户信息以及密码接口
 */
router.get('/userinfoAndPassword', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /user/userinfoAndPassword`);
    const { uid } = req.token_payload.data;
    let user;
    // 读取用户Id
    try {
        user = new ObjId(req.query.id);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "用户Id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 用户Id ${ JSON.stringify(user) }`);
    // 进入业务层
    userService.getUserInfoAndPassword(req.requestId, uid, user).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取用户信息响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取用户信息时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 注册用户接口
 * @param { string } userType    用户类型
 * @param { string } name        用户真实姓名
 * @param { string } phone       用户手机号
 * @param { string } account     用户账号
 * @param { string } password    用户密码
 */
router.post('/register', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 POST /user/register`);
    const { uid } = req.token_payload.data;
    let user;
    // 读取负责人用户数据
    try {
        user = new User(req.body);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "用户参数有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 用户参数 ${ JSON.stringify(user) }`);
    // 进入业务层
    userService.register(req.requestId, uid, user).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 注册用户响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 注册用户时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 用户权限接口
 * @pseudocode 运行逻辑:
 *                 1. 查 UserPower表 直接返回对应用户的权限
 */
// router.get('/power', function (req, res, next) {
//     console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /user/power`);
//     const { uid } = req.token_payload.data;
//     // 进入业务层
//     userService.getPower(req.requestId, uid).then(result => {
//         console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取用户权限范围响应 ${ JSON.stringify(result) }`);
//         return res.status(result.code).json(result);
//     }).catch(err => {
//         console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取用户权限范围时服务器内部错误 ${ err.message }`);
//         return res.status(500).json(new Response().fail(500, '服务器内部错误'));
//     });
// });

/**
 * @apiName 获取全量用户接口
 * @pseudocode 运行逻辑:
 *                 1. 这种一眼可以看见所有用户的只有管理员才有权限
 */
router.get('/userList', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /user/userList`);
    const { uid } = req.token_payload.data;
    // 进入业务层
    userService.getAllUser(req.requestId, uid).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有用户信息响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有用户信息时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 获取全量用户以及密码接口
 * @pseudocode 运行逻辑:
 *                 1. 这种一眼可以看见所有用户的只有管理员才有权限
 */
router.get('/userListAndPassword', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /user/userListAndPassword`);
    const { uid } = req.token_payload.data;
    // 进入业务层
    userService.getAllUserAndPassword(req.requestId, uid).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有用户信息响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取所有用户信息时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 懒加载用户列表接口
 * @param { int } cursor  即将获取的用户ID
 * @param { int } limit   即将获取的用户数量
 * @pseudocode 运行逻辑:
 *                 1. 这种一眼可以看见所有用户的只有管理员才有权限
 */
router.get('/userListLazyload', function (req, res, next) {});

/**
 * @apiName 删除用户接口
 * @pseudocode 运行逻辑:
 *                 1. 这个接口应该不允许直接删除才对
 *                 2. 需要先检查是否有科室占用
 *                 3. 有则删除失败
 */
router.delete('/deleteUser', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 DELETE /user/deleteUser`);
    const { uid } = req.token_payload.data;
    let userId;
    try {
        userId = new ObjId(req.query.id).id;
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "用户Id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 用户Id ${ JSON.stringify(userId) }`);
    // 进入业务层
    userService.deleteUser(req.requestId, uid, userId).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 删除用户响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 删除用户时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 修改用户信息接口
 */
router.put('/updateUserInfo', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 PUT /user/updateUserInfo`);
    const { uid } = req.token_payload.data;
    let user;
    try {
        user = new UserUpdateInfo(req.body);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "用户参数有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 用户参数 ${ JSON.stringify(user) }`);
    // 进入业务层
    userService.updateUserInfo(req.requestId, uid, user).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 修改用户信息响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 修改用户信息时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 修改用户信息以及密码接口
 */
router.put('/updateUserInfoAndPassword', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 PUT /user/updateUserInfoAndPassword`);
    const { uid } = req.token_payload.data;
    let user;
    try {
        user = new UserUpdateInfoAndPassword(req.body);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "用户参数有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 用户参数 ${ JSON.stringify(user) }`);
    // 进入业务层
    userService.updateUserInfoAndPassword(req.requestId, uid, user).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 修改用户信息响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 修改用户信息时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 修改当前账号信息以及密码接口
 */
router.put('/updateSelfInfoAndPassword', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 PUT /user/updateSelfInfoAndPassword`);
    const { uid } = req.token_payload.data;
    let user;
    try {
        user = new UserInfoUpdate(req.body);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "用户参数有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 用户参数 ${ JSON.stringify(user) }`);
    // 进入业务层
    userService.updateSelfInfoAndPassword(req.requestId, uid, user).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 修改用户信息响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 修改用户信息时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 获取用户负责的科室列表接口
 */
router.get('/userDepartment', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /user/userDepartment`);
    const { uid } = req.token_payload.data;
    let user;
    // 读取用户Id
    try {
        user = new ObjId(req.query.id);
    } catch (err) {
        return res.status(400).json(new Response().fail(400, "用户Id有误"));
    }
    console.log(`[ INFO ] [ req-id = ${ req.requestId } ] [ Controller ] 用户Id ${ JSON.stringify(user) }`);
    // 进入业务层
    userService.getUserDepartment(req.requestId, uid, user).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取用户负责的科室列表响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取用户负责的科室列表时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 获取当前登录用户负责的科室列表接口
 */
router.get('/selfDepartment', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /user/selfDepartment`);
    const { uid } = req.token_payload.data;
    // 进入业务层
    userService.getSelfDepartment(req.requestId, uid).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取用户负责的科室列表响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取用户负责的科室列表时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

/**
 * @apiName 获取当前用户的权限列表
 */
router.get('/power', function (req, res, next) {
    console.log(`[ REQUEST ] [ req-id = ${ req.requestId } ] [ Controller ] 请求进入 GET /user/power`);
    const { uid } = req.token_payload.data;
    // 进入业务层
    userService.getSelfPowerList(req.requestId, uid).then(result => {
        console.log(`[ RESPONSE ] [ req-id = ${ req.requestId } ] [ Controller ] 获取当前用户权限列表响应 ${ JSON.stringify(result) }`);
        return res.status(result.code).json(result);
    }).catch(err => {
        console.log(`[ ERROR ] [ req-id = ${ req.requestId } ] [ Controller ] 获取当前用户权限列表时服务器内部错误 ${ err.message }`);
        return res.status(500).json(new Response().fail(500, '服务器内部错误'));
    });
});

module.exports = router;